import 'package:flutter/material.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/leaderBoardDatagrid.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';

class LeaderBoardClassTab extends StatefulWidget {
  @override
  State<LeaderBoardClassTab> createState() => _LeaderBoardClassTabState();
}

class _LeaderBoardClassTabState extends State<LeaderBoardClassTab> {
  final UserController _userController = Get.find();
  final box = Hive.box('appconfig');

  @override
  void initState() {
    bool isStudent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Student;
    if (isStudent) {
      _userController.fetchMyClassesForStuent();

      Future.delayed(Duration(milliseconds: 200), (() {
        String _classId = box.get(CacheManagerKey['currentclass'].toString());
        if (_classId != '') {
          _userController.getLeaderBoardClass(_classId);
        }
      }));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build LeaderBoardClassTab');

    return Container(
      child: Center(
        child: _userController.obx((state)=>
         LeaderBoardDataGrids(_userController.rankingsClass),
        onEmpty: Text('Leader board may be disabled'.tr),
        )
      ),
    );
  }
}
